<?php

/**
 * Framework_DB_DaoCollectionPager
 *
 * @author Application Support Group
 * @package BAMF
 * @todo Keep tabs on PHP development to see if attribute 'auxil' can ever be used
*/
class Framework_DB_DaoParameters extends Framework_Object {

	protected $_attributes ;

	public function __construct( $initParams=array() ) {
		parent::__construct();
		
		$this->_attributes = array( 
			'sortvar' => '',
			'sortdir' => '',
			'whereOperator' => 'AND',
			'where' => ' ',
			'rawSearchParams' => '',
			'fields' => array(),

			'values' => array(),
/* 			, */
/* 			'auxil'=> array() */
		);

		$initKeys = array_keys( $initParams ) ;
		rsort( $initKeys ) ;
		for( $i=0; $i<count( $initKeys ); $i++ ) {
			$nextKey = $initKeys[$i] ;

			if( $nextKey == 'sortvar' ) { $this->sortvar = $initParams[$nextKey] ; } 
			elseif( $nextKey == 'sortdir' ) { $this->sortdir = $initParams[$nextKey] ; } 
			elseif( $nextKey == 'whereOperator' ) { $this->whereOperator = $initParams[$nextKey] ; } 
			elseif( $nextKey == 'where' ) { 
				$whereArray = $initParams[$nextKey] ; 
				$whereFragment = " " ;
		
				if( count( $whereArray ) > 0 ) {
					$whereFragment = " WHERE " ;
				}
				
				for( $j=0; $j<count($whereArray); $j++ ) {
					$whereFragment .= $whereArray[$j] ;

					if(SHOW_DEBUG && 

							(strpos($whereFragment, "'") !== false ||

							strpos($whereFragment, '"') !== false)) {

						$backtrace = debug_backtrace();

						$file = $backtrace[0]['file'];

						$line = $backtrace[0]['line'];

						$fixedWhere = preg_replace('/([\'"]).*?\1/', '?', $whereArray[$j]);

						trigger_error("You appear to be using quotes in the WHERE clause of the DaoParameters object created in <b>{$file}</b> on line <b>{$line}</b>.<br/>".

								"Rather than specifying values directly in your where clause, which can create a SQL injection opening, " . 

								"replace the offending clause of your 'where' array with this:<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$fixedWhere</p> " . 

								"and add a new argument to your DaoParameters object called 'values' which takes in an array of the values to substitute for the question marks.<br/>".

								"Triggered ", E_USER_WARNING) ;

					}
					if( $j < (count( $whereArray ) - 1)) {
						$whereFragment .= " {$this->whereOperator} " ;
					}
				}
		
				$whereFragment .= " " ;
				$this->where = $whereFragment;

			} elseif( $nextKey == 'fields' ) {

				$this->fields = $initParams[$nextKey] ;

			} elseif( $nextKey == 'values' ) {

				$this->values = $initParams[$nextKey] ;
			} else {

					$backtrace = debug_backtrace();

					$file = $backtrace[0]['file'];

					$line = $backtrace[0]['line'];
					trigger_error("Unknown property <b>{$nextKey}</b> for DaoParameters object created in <b>{$file}</b> on line <b>{$line}</b>. <br/>Triggered ", E_USER_WARNING) ;

				
			}
		}
		
		
		

	}// end constructor



	public function __get( $anAttribute ) {

		$_output = false ;

		if( array_key_exists($anAttribute, $this->_attributes  )) {
			$_output = $this->_attributes[$anAttribute] ;
/* 		} elseif( isset( $this->_attributes['auxil'][$anAttribute] )) { */
/* 			$_output = $this->_attributes['auxil'][$anAttribute] ; */
		} else {
			$_output = Framework_ErrorHandler::raiseError( "Cannot get the requested property.", __METHOD__."::".__LINE__ ) ;
		}

		return $_output ;
	}



	public function __set( $anAttribute, $aValue ) {

		$_output = false ;


		if( array_key_exists($anAttribute, $this->_attributes  )) {
			$this->_attributes[$anAttribute] = $aValue ;
			$_output = true ;
/* 		} elseif( isset( $this->_attributes['auxil'][$anAttribute] )) { */
/* 			$this->_attributes['auxil'][$anAttribute] = $aValue ; */
/* 			$_output = true ; */
		} else {
			$_output = Framework_ErrorHandler::raiseError( "Cannot set the requested property.", __METHOD__."::".__LINE__ ) ;
		}

		return $_output ;
	}


	/**
	 * buildWhereInSql
	 *
	 * Assemble an SQL WHERE clause from a DaoCollection object or a values array
	 *
	 * @param $dataSet mixed The collection to use or.
	 * @param $fieldName string The field that contains values to add to the WHERE cluase
	 *
	 * @return string
	 *
	 * @access public
	 */ 

	public static function buildWhereInSql( $dataSet, $fieldName ) {
		$_output = "" ;
		$theDataSet = null ;
		$dsIsValid = false ;

		// dataSet is a DaoCollection
		if( $dataSet instanceof Framework_DB_DaoCollection && !empty( $dataSet->_data )) {
			$theDataSet = array_unique( array_values( $dataSet->getLookupArray( $fieldName ))) ;
			$dsIsValid = true ;
		}

		// dataSet is a values array
		if( !empty( $dataSet ) && is_array( $dataSet )) {
			$theDataSet = array_unique( array_values( $dataSet )) ;
			$dsIsValid = true ;
		}

		if( !empty( $fieldName ) && $dsIsValid ) {
			$_output .= "$fieldName IN (" ;

			for( $i=0; $i<count( $theDataSet ); $i++ ) {
				$_output .= $theDataSet[$i] ;

				if( $i < ( count( $theDataSet ) - 1)) {
					$_output .= "," ;
				}
			}

			$_output .= ")" ;
		}

		return $_output ;

	}// end buildWhereInSql



	public function __destruct() {
		parent::__destruct();
	}



	public function __toString() {

		return __CLASS__;

	}
}

?>
